// pages/index/index.js
const Page = require('../../../utils/ald-stat.js').Page;
const util = require('../../../utils/throttle.js');
const app = getApp();
const innerAudioContext = wx.createInnerAudioContext();
let videoAd = null;
let interstitialAd = null;
const ageArr = [],
  heightArr = [],
  weightArr = []
for (let i = 1; i < 101; i++) {
  ageArr.push(i)
}
for (let i = 120; i < 220; i++) {
  heightArr.push(i)
}
for (let i = 30; i < 180; i++) {
  weightArr.push(i)
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    clicked: true,
    measure: true,
    clim: true,
    zanwu: true,
    weighing: true,
    weighclick: true,
    perMessage: true,
    genderArray: ['男', '女'],
    ageArr: ageArr,
    heightArr: heightArr,
    weightArr: weightArr,
    weekss: ['../../../imgs/daya.png', '../../../imgs/dayb.png', '../../../imgs/dayc.png', '../../../imgs/dayd.png', '../../../imgs/daye.png', '../../../imgs/dayf.png', '../../../imgs/dayg.png'],
    tryfail: true,
    trysuc: true,
    stepdh: true,
    luck: true,
    tjjl: true,
    gzjl: true,
    addmy: true,
    contact: true,
    zllqcg: true,
    zlclickedcc: true,
    sign: true,
    step: '获取步数',
    zl_goods: '',
    jx_goods: '',
    xr_goods: '',
    free_goods: '',
    userInfo: '',
    share: true,
    shouquan: true,
    shouIndex: true,
    openid: '',
    page: 1,
    haveMore: true,
    noMore: '',
    newUser: true,
    newuserclick: true,
    isOpenWXRun: true,
    isOpenWXRuns: true, //为了区分运动授权，打开运动设置
    is_dh: '',
    goods_id: '',
    zl: '',
    jx: '',
    total_currency: '',
    today_currency: '',
    today_rlb: '',
    tomorrow_rlb: '',
    dhcg: false,
    bespoke: false,
    isHaveopenid: true,
    bj_picture: '',
    notice_title: '',
    dayed: 1,
    x_times: '',
    noluck: false,
    money: '',
    ad_data: {},
    plate: '',
    number: '',
    clickedfail: true,
    clickedsuc: true,
    erweima: '',
    seat: '', //签到12，步数兑换11,幸运奖励13
    ra: '',
    home_lucky_ad: '',
    flow: false, //防止流量主广告点击回来出弹框
    result: '',
    cj: '',
    cj_id: '',
    sharechou: '',
    signfail: true,
    signbiao: '',
    form_id: '',
    everything: {
      is_add: '',
      is_challenge: '',
      is_follow: '',
      is_lucky_draw: '',
      is_new: '',
      is_qun: '',
      is_red: '',
      is_rule: '',
      is_sign: '',
      is_turntable: '',
      share_act:'',
    },
    ad_id:'',
    is_chouswitch:true,
    isShowInterstitialAd:1,
    step_adv_type:1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    wx.showLoading({
      title: '加载中',
    })
    const that = this;
    if (wx.createInterstitialAd) {
      interstitialAd = wx.createInterstitialAd({
        adUnitId: 'adunit-deaf9f2dbf0fe8a3'
      })
      interstitialAd.onError((err) => {
        console.log('onError event emit', err)
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
      })
    }
    if (wx.createRewardedVideoAd) {
      videoAd = wx.createRewardedVideoAd({
        adUnitId: 'adunit-d760aa330a1e1bcb'
      });
      videoAd.load();
      videoAd.onError(function(res) {
        console.log(101, res)
        if (res.errMsg == 'no advertisement' || res.errMsg == 'no ad unit id' || res.errCode == 1004) {
          that.setData({
            noluck: true,
          })
        }
      })
      videoAd.onClose((status) => {
        if (status && status.isEnded) {
          if (that.data.signbiao == 333) {
            var object = {
              unionid: wx.getStorageSync('openid'),
            }
            var sign = app.sort_ASCII(object)
            wx.request({
              url: app.globalData.new_url + '/userinfo/Userinfo/sign_ad',
              data: {
                unionid: wx.getStorageSync('openid'),
                sign: sign
              },
              method: 'POST',
              success: function(res) {
                console.log('签到', res)
                that.music();
                if (res.data.code === 200) {
                  that.setData({
                    sign: false,
                    today_rlb: res.data.today_rlb,
                    tomorrow_rlb: res.data.receive_rlb,
                    dayed: res.data.count_day,
                  })
                }
                that.onShow();
              }
            })
          } else {
            that.music();
            var object = {
              unionid: wx.getStorageSync('openid'),
            }
            var sign = app.sort_ASCII(object)
            wx.request({
              url: app.globalData.new_url + '/userinfo/Userinfo/incentive',
              data: {
                unionid: wx.getStorageSync('openid'),
                sign: sign
              },
              method: 'POST',
              success: function(res) {
                console.log(321, res)
                that.onShow();
              }
            })
          }
        } else {
          if (that.data.signbiao == 333) {
            that.setData({
              signfail: false,
            })
          }
        }
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
      })
    }
    // if (options.gzjl == 100 && wx.getStorageSync('openid')) {
    //   wx.request({
    //     url: app.globalData.base_url + '/follow_wechat',
    //     data: {
    //       openid: wx.getStorageSync('openid')
    //     },
    //     success: function(res) {
    //       if (res.data.wechat) {
    //         console.log('已领取')
    //       } else {
    //         wx.showModal({
    //           title: '领取成功',
    //           content: '关注任务已完成，您的10热力币已到账',
    //           showCancel: false,
    //         })
    //       }
    //     }
    //   })
    // }
    if (options.jx == 55) {
      that.setData({
        jx: options.jx,
        openid: options.openid
      })
    }
    if (options.xr == 77) {
      that.setData({
        openid: options.openid,
        goods_id: options.goods_id,
        xr: options.xr,
      })
    }
    if (options.zl == 66) {
      that.setData({
        zl: 66,
        goods_id: options.goods_id,
        openid: options.openid
      })
      if (options.openid === wx.getStorageSync('openid')) {
        wx.navigateTo({
          url: '/pages/main/oneDetail/index?id=' + options.goods_id + '&&parements=ss',
        })
      } else if (wx.getStorageSync('openid')) {
        wx.navigateTo({
          url: '/pages/other/zhuli/index?goods_id=' + options.goods_id + '&&openid=' + options.openid,
        })
      } else {
        that.setData({
          newUser: false,
          newuserclick: true,
        })
      }
    }
    if (options.cj == 123) {
      that.data.cj = options.cj;
      that.data.cj_id = options.id;
      that.data.sharechou = options.share;
      that.data.openid = options.openid
      if (options.openid === wx.getStorageSync('openid')) {
        wx.navigateTo({
          url: '/pages/other/choudetail/index?id=' + options.id,
        })
      } else if (wx.getStorageSync('openid')) {
        wx.navigateTo({
          url: '/pages/other/choudetail/index?id=' + options.id + '&&openid=' + options.openid + '&&share=' + options.share,
        })
      } else {
        that.setData({
          newUser: false,
          newuserclick: true,
        })
      }
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function(cb) {
    var that = this;
    if (that.data.isShowInterstitialAd==100){
      if (interstitialAd) {
        interstitialAd.show().catch((err) => {
          console.error(err)
        })
      }
    }
    if (that.data.erweima == 3) {
      that.setData({
        clickedsuc: true,
        trysuc: false,
      })
      if (that.data.plate == 1) {
        var object = {
          unionid: wx.getStorageSync('openid'),
        }
        var sign = app.sort_ASCII(object)
        wx.request({
          url: app.globalData.new_url + '/Challenge/Challenge/is_reach',
          data: {
            unionid: wx.getStorageSync('openid'),
            sign: sign
          },
          method: 'POST',
          success: function(res) {
            that.setData({
              step: 0
            })
          }
        })
      }
      that.data.erweima = 0
    } else if (that.data.erweima == 4) {
      that.setData({
        clickedfail: true,
        tryfail: false,
      })
      that.data.erweima = 0
    }
    if (app.globalData.scene === 1038 && that.data.flow) {
      if (that.data.plate == 1) {
        var ratio = that.data.x_times;
        var step = that.data.step;
      } else {
        var step = "",
          ratio = "";
      }
      var ra = that.data.ra;
      var object = {
        unionid: wx.getStorageSync('openid'),
        type: 2,
        ad_id: that.data.ad_id,
        plate: that.data.plate,
        ra: ra,
        ratio: ratio,
        step: step
      }
      var sign = app.sort_ASCII(object)
      wx.request({
        url: app.globalData.new_url + '/adauth/Adv/back_adv',
        data: {
          unionid: wx.getStorageSync('openid'),
          type: 2,
          ad_id: that.data.ad_id,
          plate: that.data.plate,
          ra: ra,
          ratio: ratio,
          step: step,
          sign:sign
        },
        method: 'POST',
        header: {
          'content-type': 'application/json'
        },
        success: function(res) {
          console.log(res.data.msg, res)
          that.data.erweima = 0;
          that.data.flow = false;
          app.globalData.scene = 1001;
          if (res.data.code===200) {
            if (that.data.plate == 1) {
              var object = {
                unionid: wx.getStorageSync('openid'),
              }
              var sign = app.sort_ASCII(object)
              wx.request({
                url: app.globalData.new_url + '/Challenge/Challenge/is_reach',
                data: {
                  unionid: wx.getStorageSync('openid'),
                  sign: sign
                },
                method: 'POST',
                success: function(res) {
                  that.setData({
                    step: 0
                  })
                }
              })
            }
            that.setData({
              number: res.data.content,
              trysuc: false,
              clickedsuc: true,
            })
          } else {
            that.setData({
              tryfail: false,
              clickedfail: true
            })
          }
        }
      })
    }
    if (app.globalData.scene === 1089) {
      var object = {
        unionid: wx.getStorageSync('openid'),
      }
      var sign = app.sort_ASCII(object)
      wx.request({
        url: app.globalData.new_url + '/userinfo/Userinfo/add_wechat',
        data: {
          unionid: wx.getStorageSync('openid'),
          sign: sign
        },
        method: 'POST',
        header: {
          'content-type': 'application/json'
        },
        success: function(res) {
          if (res.data.code === 200) {
            wx.showModal({
              title: '领取成功',
              content: '添加到我的任务已完成，10热力币已到账',
              showCancel: false,
              success(res) {
                if (res.confirm) {
                  that.setData({
                    'everything.is_add': 1
                  })
                }
              }
            })
          }
        }
      })
    }
    if (wx.getStorageSync('openid') && wx.getStorageSync('open_id') != 0) {
      that.setData({
        isHaveopenid: true
      })
      that.onRun();
      var openids = wx.getStorageSync('openid');
      if (that.data.jx == 55) {
        var object = {
          hy_unionid: wx.getStorageSync('openid'),
          unionid: that.data.openid,
          goods_id: 0,
        }
        var sign = app.sort_ASCII(object)
        wx.request({
          url: app.globalData.new_url + '/userinfo/Userinfo/share',
          data: {
            hy_unionid: wx.getStorageSync('openid'),
            unionid: that.data.openid,
            goods_id: 0,
            sign: sign
          },
          method: 'POST',
          success: function(res) {
            console.log('jx', res)
          }
        })
      }
      if (that.data.xr == 77) {
        var object = {
          hy_unionid: wx.getStorageSync('openid'),
          unionid: that.data.openid,
          goods_id: that.data.goods_id,
        }
        var sign = app.sort_ASCII(object)
        wx.request({
          url: app.globalData.new_url + '/userinfo/Userinfo/share',
          data: {
            hy_unionid: wx.getStorageSync('openid'),
            unionid: that.data.openid,
            goods_id: that.data.goods_id,
            sign: sign
          },
          method: 'POST',
          success: function(res) {

          }
        })
      }
    } else {
      var openids = 0;
      that.setData({
        newUser: false,
        isHaveopenid: false
      })
    }
    var sobject = {
      unionid: openids,
    }
    var ssign = app.sort_ASCII(sobject)
    wx.request({
      url: app.globalData.new_url + '/userinfo/Userinfo/home_content',
      data: {
        unionid: openids,
        sign: ssign
      },
      method: 'POST',
      header: {
        'Cache-Control': 'max-age=60,public', //60秒
      },
      success: function(res) {
        wx.hideLoading();
        console.log(res)
        that.setData({
          everything: res.data.con,
          total_currency: res.data.user.total_currency,
          today_currency: res.data.user.today_currency,
          home_lucky_ad: res.data.home_lucky_ad,
          result: res.data.weight_data,
          is_chouswitch: res.data.is_block,
          step_adv_type: res.data.step_adv_type
        })
        that.data.ad_id = res.data.step_ad,
          typeof cb == "function" && cb(that.data.everything.is_new);
        if (res.data.scene_value == 1 && that.data.jx != 55 && that.data.xr != 77) {
          if (res.data.weight_data.type_id == 2) {
            that.setData({
              zanwu: false,
            })
          } else {
            that.setData({
              measure: false,
            })
          }
        }
      }
    });
  },
  onHide: function() {
    const that = this;
    that.setData({
      addmy: true,
      newuserclick: false,
    })
    that.data.isShowInterstitialAd = 1
    app.globalData.scene = 1001;
  },
  onReady: function() {
    const that = this;
    if (wx.getStorageSync('openid')) {
      var openid = wx.getStorageSync('openid');
    } else {
      var openid = 0;
    }
    var sobject = {
      unionid: openid,
    }
    var ssign = app.sort_ASCII(sobject)
    wx.request({
      url: app.globalData.new_url + '/userinfo/Userinfo/down_home_content',
      data: {
        unionid: openid,
        sign: ssign
      },
      method:'POST',
      header: {
        'Cache-Control': 'max-age=60,public', //60秒
      },
      success: function(res) {
        console.log(res)
        that.setData({
          notice_title: res.data.notice_title,
          bj_picture: res.data.bj_picture,
          xr_goods: res.data.xr_goods,
          zl_goods: res.data.zl_goods,
          jx_goods: res.data.jx_goods,
        })
      }
    })

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    let that = this;
    wx.showNavigationBarLoading() //在标题栏中显示加载
    that.onShow(); // 刷新页面
    wx.hideNavigationBarLoading() //完成停止加载
    wx.stopPullDownRefresh() //停止下拉刷新
  },

  sportSQS: function() {
    var that = this;
    wx.getWeRunData({
      fail: function(res) {
        that.setData({
          isOpenWXRuns: false,
        })
      },
      complete: function() {
        that.onShow()
      },
    })
  },
  music: function() {
    innerAudioContext.src = 'https://www.mnancheng.com/Public/home/bj_music.mp3'
    innerAudioContext.play()
  },

  stepchange: util.throttle(function(e) {
    const that = this
    that.data.form_id = e.detail.formId;
    that.collectFormid();
    var step = that.data.step;
    var money = (step / 5000).toFixed(2);
    var object = {
      unionid: wx.getStorageSync('openid'),
      ad_id: 0,
      type: 1,
      step_num: step,
    }
    var sign = app.sort_ASCII(object)
    console.log(sign)
    wx.request({
      url: app.globalData.new_url + '/userinfo/Userinfo/exchange',
      data: {
        step_num: step,
        ad_id: 0,
        type: 1,
        unionid: wx.getStorageSync('openid'),
        sign: sign
      },
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      success: function(res) {
        console.log('放弃翻倍', res)
        var object = {
          unionid: wx.getStorageSync('openid'),
        }
        var sign = app.sort_ASCII(object)
        wx.request({
          url: app.globalData.new_url + '/Challenge/Challenge/is_reach',
          data: {
            unionid: wx.getStorageSync('openid'),
            sign: sign
          },
          method: 'POST',
          success: function(res) {
            that.music();
            that.setData({
              money: money,
              step: 0,
              share: false,
              stepdh: true,
            })
            if (!that.data.share) {
              that.onShow();
            }
          }
        })
      }
    })
  }, 1500),
  exchange: util.throttle(function(e) {
    const that = this
    that.data.form_id = e.detail.formId;
    that.collectFormid();
    var is_dh = that.data.is_dh;
    var step = that.data.step;
    var money = (step / 5000).toFixed(2);
    that.data.ra = money;
    that.setData({
      dhcg: true,
    })
    if (step < 50) {
      wx.showModal({
        content: '可兑换步数低于50无法兑换热力币，多走些步数再来兑换吧',
        showCancel: false,
        success(res) {
          if (res.confirm) {
            that.setData({
              dhcg: false,
            })
          }
        }
      })
    } else if (is_dh == 1) {
      wx.showModal({
        title: '热力币兑换',
        content: `你将消耗${step}步，兑换${money}个热力币，确认兑换？`,
        success: function(res) {
          if (res.confirm) {
            if (that.data.ad_id) {
              var ad_id = that.data.ad_id;
              var object = {
                unionid: wx.getStorageSync('openid'),
                ad_id: ad_id,
                type: 2,
                step_num: step,
              }
              var sign = app.sort_ASCII(object)
              wx.request({
                url: app.globalData.new_url + '/userinfo/Userinfo/exchange',
                data: {
                  step_num: step,
                  ad_id: ad_id,
                  type: 2,
                  unionid: wx.getStorageSync('openid'),
                  sign: sign
                },
                method: 'POST',
                header: {
                  'content-type': 'application/json'
                },
                success: function (res) {
                  console.log(123, res)
                  that.setData({
                    stepdh: false,
                    x_times: res.data.x_times,
                  })
                  if (res.data.ad) {
                    that.setData({
                      ad_data: res.data.ad
                    })
                  }
                }
              })
            } else {
              var object = {
                unionid: wx.getStorageSync('openid'),
                ad_id: 0,
                type: 1,
                step_num: step,
              }
              var sign = app.sort_ASCII(object)
              wx.request({
                url: app.globalData.new_url + '/userinfo/Userinfo/exchange',
                data: {
                  step_num: step,
                  ad_id: 0,
                  type: 1,
                  unionid: wx.getStorageSync('openid'),
                  sign: sign
                },
                method: 'POST',
                header: {
                  'content-type': 'application/json'
                },
                success: function (res) {
                  var object = {
                    unionid: wx.getStorageSync('openid'),
                  }
                  var sign = app.sort_ASCII(object)
                  wx.request({
                    url: app.globalData.new_url + '/Challenge/Challenge/is_reach',
                    data: {
                      unionid: wx.getStorageSync('openid'),
                      sign: sign
                    },
                    method: 'POST',
                    success: function (res) {
                      that.music();
                      that.setData({
                        money: money,
                        step: 0,
                        share: false,
                        stepdh: true,
                      })
                      if (!that.data.share) {
                        that.onShow();
                      }
                    }
                  })
                }
              })
            }
          } else if (res.cancel) {
            that.setData({
              dhcg: false,
            })
          }
        }
      })
    } else if (is_dh == 2) {
      wx.showModal({
        content: '今日步数兑换已达上限，明天再兑换吧',
        showCancel: false,
        success(res) {
          if (res.confirm) {
            that.setData({
              dhcg: false,
            })
          }
        }
      })
    } else {
      wx.showModal({
        content: '暂无获取到步数，请重新获取',
        showCancel: false,
        success(res) {
          if (res.confirm) {
            that.setData({
              dhcg: false,
            })
            that.onShow();
          }
        }
      })
    }
  }, 1500),
  onRun: function(cb) {
    var that = this;
    wx.getSetting({
      success(res) {        
        if (!res.authSetting['scope.werun']) {
          that.setData({
            isOpenWXRun: false,
          })
        } else {
          if (!wx.getStorageSync('session')) {
            that.setData({
              isOpenWXRun: false,
            })
          } else {
            wx.checkSession({
              success: function() {
                wx.getWeRunData({
                  success(res) {
                    var object = {
                      encryptedData: encodeURIComponent(res.encryptedData),
                      iv: encodeURIComponent(res.iv),
                      session: wx.getStorageSync('session'),
                      unionid: wx.getStorageSync('openid'),
                    }
                    var sign = app.sort_ASCII(object)
                    wx.request({
                      url: app.globalData.new_url + '/user/login/wxrun',
                      data: {
                        encryptedData: encodeURIComponent(res.encryptedData),
                        iv: encodeURIComponent(res.iv),
                        session: wx.getStorageSync('session'),
                        unionid: wx.getStorageSync('openid'),
                        sign:sign
                      },
                      method: 'POST',
                      header: {
                        'Content-Type': 'application/x-www-form-urlencoded'
                      },
                      success: function(res) {
                        console.log('4',res)
                        if (res.data.status == 1) {
                          that.data.is_dh = res.data.is_dh;
                          that.setData({
                            isOpenWXRun: true,
                            step: res.data.data,
                          })
                        } else {
                          that.setData({
                            isOpenWXRun: false,
                          })
                          wx.login({
                            success: res => {
                              wx.getUserInfo({
                                withCredentials: true,
                                success: function(res_user) {
                                  var object = {
                                    record_type: 1,
                                    auth_type: 0,
                                    scene_value: 0,
                                    code: res.code,
                                    encryptedData: encodeURIComponent(res_user.encryptedData),
                                    iv: encodeURIComponent(res_user.iv),
                                  }
                                  var sign = app.sort_ASCII(object)
                                  wx.request({
                                    url: app.globalData.new_url + '/user/login/login',
                                    data: {
                                      record_type: 1,
                                      auth_type: 0,
                                      scene_value: 0,
                                      code: res.code,
                                      encryptedData: encodeURIComponent(res_user.encryptedData),
                                      iv: encodeURIComponent(res_user.iv),
                                      sign: sign,
                                    },
                                    method: 'POST',
                                    header: {
                                      'content-type': 'application/json'
                                    },
                                    success: function(res) {
                                      wx.setStorageSync('session', res.data.hash);
                                      wx.setStorageSync('openid', res.data.openid);
                                      wx.setStorageSync('open_id', res.data.open_id);
                                      that.onShow();
                                    }
                                  })
                                },
                              })
                            },

                          })
                        }
                      }
                    })
                  }
                })
              },
              fail: function() {
                wx.login({
                  success: res => {
                    if (res.code) {
                      wx.getUserInfo({
                        withCredentials: true,
                        success: function(res_user) {
                          var object = {
                            record_type: 1,
                            auth_type: 0,
                            scene_value: 0,
                            code: res.code,
                            encryptedData: encodeURIComponent(res_user.encryptedData),
                            iv: encodeURIComponent(res_user.iv),
                          }
                          var sign = app.sort_ASCII(object)
                          wx.request({
                            url: app.globalData.new_url + '/user/login/login',
                            data: {
                              record_type: 1,
                              auth_type: 0,
                              scene_value: 0,
                              code: res.code,
                              encryptedData: encodeURIComponent(res_user.encryptedData),
                              iv: encodeURIComponent(res_user.iv),
                              sign: sign,
                            },
                            method: 'POST',
                            header: {
                              'content-type': 'application/json'
                            },
                            success: function(res) {
                              wx.setStorageSync('session', res.data.hash);
                              wx.setStorageSync('openid', res.data.openid);
                              wx.setStorageSync('open_id', res.data.open_id);
                              that.onShow();
                            }
                          })
                        },
                      })
                    } else {
                      console.log('获取用户登录态失败！' + res.errMsg)
                    }
                  },
                })
              }
            })
          }
        }
      }
    })
  },
  navigateToAd: function(e) {
    const that = this;
    that.data.seat = e.target.dataset.seat;
    const plate = e.target.dataset.plate;
    that.data.plate = plate;
    const ad_data = that.data.ad_data;
    const ad_id = ad_data.ad_id;
    var x_times = that.data.x_times;
    var ra = that.data.ra;
    that.data.ra = ra;
    if (ad_data.way == 1) {
      wx.navigateToMiniProgram({
        appId: ad_data.appid,
        path: ad_data.path,
        success(res) {
          var object = {
            unionid: wx.getStorageSync('openid'),
            type: 1,
            ad_id: ad_id,
            plate: plate,
            ra: ra,
            ratio: x_times
          }
          var sign = app.sort_ASCII(object)
          wx.request({
            url: app.globalData.new_url + '/adauth/Adv/back_adv',
            data: {
              unionid: wx.getStorageSync('openid'),
              type: 1,
              ad_id: ad_id,
              plate: plate,
              ra: ra,
              ratio: x_times,
              sign:sign
            },
            method: 'POST',
            header: {
              'content-type': 'application/json'
            },
            success: function(res) {
              console.log('跳转成功', res)
              that.setData({
                dhcg: false,
                sign: true,
                stepdh: true,
                luck: true,
              })
              that.data.flow = true;
            }
          })
        }
      })
    } else {
      if (plate == 1) {
        var step = that.data.step;
        wx.navigateTo({
          url: '../../other/erweima/index?ad_id=' + ad_id + '&&plate=' + plate + '&&ra=' + ra + '&&x_times=' + x_times + '&&step=' + step,
        })
      } else {
        wx.navigateTo({
          url: '../../other/erweima/index?ad_id=' + ad_id + '&&plate=' + plate + '&&rand_rlb=' + ra,
        })
      }
      that.setData({
        sign: true,
        stepdh: true,
        dhcg: false,
        luck: true,
      })
    }
  },
  goSignForm: util.throttle(function(e) {
    const that = this;
    that.data.form_id = e.detail.formId;
    that.collectFormid();
    if (that.data.noluck) {
      var object = {
        unionid: wx.getStorageSync('openid'),
      }
      var sign = app.sort_ASCII(object)
      wx.request({
        url: app.globalData.new_url + '/userinfo/Userinfo/sign_ad',
        data: {
          unionid: wx.getStorageSync('openid'),
          sign: sign
        },
        method: 'POST',
        success: function(res) {
          console.log('签到', res)
          that.music();
          if (res.data.code === 200) {
            that.setData({
              sign: false,
              today_rlb: res.data.today_rlb,
              tomorrow_rlb: res.data.receive_rlb,
              dayed: res.data.count_day,
            })
          }
          that.onShow();
        }
      })
    } else {
      that.opensignluck();
    }
  }, 1500),
  opensignluck: util.throttle(function(e) {
    const that = this;
    that.data.signbiao = 333;
    videoAd.show().catch(err => {
      //失败重试
      videoAd.load().then(() => {
        videoAd.show();
      })
    })
    that.setData({
      signfail: true,
    })
  }, 1500),
  rule: util.throttle(function(e) {
    const that = this;
    var object = {
      unionid: wx.getStorageSync('openid'),
    }
    var sign = app.sort_ASCII(object)
    wx.request({
      url: app.globalData.new_url + '/userinfo/Userinfo/rule_explain',
      data: {
        unionid: wx.getStorageSync('openid'),
        sign: sign
      },
      method: 'POST',
      success: function(res) {
        wx.navigateTo({
          url: '../../other/rule/index',
        })
      }
    })
  }, 1500),
  authorizeNows: function(e) {
    const that = this;
    if (that.data.openid) {
      app.xrLogin(function(res) {
        wx.showLoading({
          title: '授权中',
        })
        if (res) {
          wx.hideLoading();
          that.onShow();
          that.sportSQS();
          that.setData({
            shouquan: true,
            shouIndex: false,
          })
        }
        that.sportSQS;
      });
    } else {
      app.noautoLogin(function(res) {
        wx.showLoading({
          title: '授权中',
        })
        if (res) {
          wx.hideLoading();
          that.onShow();
          that.sportSQS();
          that.setData({
            newUser: true,
            newuserclick: false,
          })
        }
      });
    }
  },
  authorizeNow: function(e) {
    const that = this;
    if (that.data.openid) {
      app.xrLogin(function(res) {
        wx.showLoading({
          title: '授权中',
        })
        if (res) {
          wx.hideLoading();
          that.onShow();
          that.setData({
            shouquan: true,
            shouIndex: false,
          })
        }
      });
    } else {
      app.noautoLogin(function(res) {
        wx.showLoading({
          title: '授权中',
        })
        if (res) {
          wx.hideLoading();
          that.onShow();
          that.setData({
            newUser: true,
            newuserclick: false,
          })
        }
      });
    }
  },
  zlactive: function() {
    wx.navigateTo({
      url: '../../main/zlGoods/index',
    })
  },
  newUserLingqus: function() {
    const that = this;
    if (that.data.zl == 66) {
      var goods_id = that.data.goods_id;
      var openid = that.data.openid;
      app.xrLogin(function(res) {
        wx.showLoading({
          title: '授权中',
        })
        if (res) {
          wx.hideLoading();
          wx.navigateTo({
            url: '/pages/other/zhuli/index?goods_id=' + goods_id + '&&openid=' + openid,
          })
          that.setData({
            newUser: true,
            newuserclick: false,
          })
        }
      });
    } else if (that.data.cj == 123) {
      var id = that.data.cj_id;
      var openid = that.data.openid;
      var share = that.data.sharechou;
      app.xrLogin(function(res) {
        wx.showLoading({
          title: '授权中',
        })
        if (res) {
          wx.hideLoading();
          wx.navigateTo({
            url: '/pages/other/choudetail/index?id=' + id + '&&openid=' + openid + '&&share=' + share,
          })
          that.setData({
            newUser: true,
            newuserclick: false,
          })
        }
      });
    } else if (that.data.openid) {
      app.xrLogin(function(res) {
        wx.showLoading({
          title: '授权中',
        })
        if (res) {
          wx.hideLoading();
          that.onShow();
          that.setData({
            newUser: true,
            zllqcg: false,
            newuserclick: false,
          })
        }
      });
    } else {
      app.onLogin(function(res) {
        wx.showLoading({
          title: '授权中',
        })
        if (res) {
          wx.hideLoading();
          that.onShow();
          that.setData({
            newUser: true,
            zllqcg: false,
            newuserclick: false,
          })
        }
      });
    }
  },
  //收集formid
  collectFormid: function() {
    const that = this;
    var form_id = that.data.form_id;
    var objform = {
      unionid: wx.getStorageSync('openid'),
      formid: form_id,
      plate: 3,
    }
    var signform = app.sort_ASCII(objform)
    wx.request({
      url: app.globalData.new_url + '/user/user/add_formid',
      data: {
        unionid: wx.getStorageSync('openid'),
        formid: form_id,
        plate: 3,
        sign: signform
      },
      method: 'POST',
      success: function(res) {
        console.log(res.data.msg)
      }
    })
  },
  golqnew: util.throttle(function(e) {
    const that = this;
    that.data.form_id = e.detail.formId;
    that.collectFormid();
    var object = {
      unionid: wx.getStorageSync('openid'),
    }
    var sign = app.sort_ASCII(object)
    wx.request({
      url: app.globalData.new_url + '/userinfo/Userinfo/new_welfare',
      data: {
        unionid: wx.getStorageSync('openid'),
        sign: sign
      },
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      success: function(res) {
        console.log(res)
        that.onShow();
        that.music();
        that.setData({
          zllqcg: false,
        })
      }
    })
  }, 1500),
  wsResult: util.throttle(function(e) {
    var that = this;
    var userInfo = that.data.userInfo;
    var gender = null;
    if (e.detail.value.gender > 0) {
      gender = Number.parseInt(e.detail.value.gender) + 1;
    } else if (userInfo.gender > 0) {
      gender = userInfo.gender;
    } else {
      gender = ""
    }
    var height = null;
    if (e.detail.value.height > 0) {
      height = e.detail.value.height;
    } else if (userInfo.height > 0) {
      height = userInfo.height;
    } else {
      height = ""
    }
    var weight = null;
    if (e.detail.value.weight > 0) {
      weight = e.detail.value.weight;
    } else if (userInfo.weight > 0) {
      weight = userInfo.weight;
    } else {
      weight = ""
    }
    let age = e.detail.value.age;
    let mes = "";
    if (weight === "" || weight == 0) {
      mes = "体重"
      wx.showModal({
        title: '信息不完整',
        content: `${mes}未填写，请补充`,
        showCancel: false,
        confirmText: '知道了',
        success: function(res) {}
      })
      return
    }
    if (height === "" || height == 0) {
      mes = "身高"
      wx.showModal({
        title: '信息不完整',
        content: `${mes}未填写，请补充`,
        showCancel: false,
        confirmText: '知道了',
        success: function(res) {}
      })
      return
    }
    if (gender === "" || gender == null) {
      mes = "性别"
      wx.showModal({
        title: '信息不完整',
        content: `${mes}未填写，请补充`,
        showCancel: false,
        confirmText: '知道了',
        success: function(res) {}
      })
      return
    }
    if (age === "" || age == 0) {
      mes = "年龄"
      wx.showModal({
        title: '信息不完整',
        content: `${mes}未填写，请补充`,
        showCancel: false,
        confirmText: '知道了',
        success: function(res) {}
      })
      return
    }
    var object = {
      weight: weight,
      gender: gender,
      age: age,
      height: height,
      unionid: wx.getStorageSync('openid'),
    }
    var sign = app.sort_ASCII(object)
    wx.request({
      url: app.globalData.new_url + '/weighing/weighing/new_save_info',
      data: {
        weight: weight,
        gender: gender,
        age: age,
        height: height,
        unionid: wx.getStorageSync('openid'),
        sign: sign
      },
      method: 'POST',
      success: function(res) {
        wx.navigateTo({
          url: '../../main/analysis/index',
        })
        that.setData({
          perMessage: true,
        })
      }
    })
  }, 1500),
  pickGender: function(e) {
    this.setData({
      [`userInfo.gender`]: Number.parseInt(e.detail.value) + 1
    })
  },
  pickAge: function(e) {
    this.setData({
      [`userInfo.age`]: Number.parseInt(e.detail.value) + 1
    })
  },
  pickHeight: function(e) {
    this.setData({
      [`userInfo.height`]: Number.parseInt(e.detail.value) + 120
    })
  },
  pickWeight: function(e) {
    this.setData({
      [`userInfo.weight`]: Number.parseInt(e.detail.value) + 30
    })
  },
  analysis: util.throttle(function() {
    const that = this;
    var object = {
      unionid: wx.getStorageSync('openid'),
    }
    var sign = app.sort_ASCII(object)
    if (wx.getStorageSync('openid') && wx.getStorageSync('open_id') != 0) {
      wx.request({
        url: app.globalData.new_url + '/weighing/Weighing/see_test_info',
        data: {
          unionid: wx.getStorageSync('openid'),
          sign: sign
        },
        method: 'POST',
        header: {
          'content-type': 'application/json'
        },
        success: function(res) {
          console.log(res)
          if (res.data.code === 200) {
            wx.navigateTo({
              url: '../../main/analysis/index',
            })
          } else {
            that.setData({
              userInfo: res.data.res,
              perMessage: false,
            })
          }
        }
      })
    } else {
      that.authorizeNow();
    }
  }, 1500),
  goHeat: util.throttle(function(e) {
    var that = this;
    if (wx.getStorageSync('openid') && wx.getStorageSync('open_id') != 0) {
      wx.navigateTo({
        url: '../../main/heatMoney/index',
      })
    } else {
      that.authorizeNow();
    }
  }, 2000),
  opengchou: function() {
    wx.navigateTo({
      url: '/pages/other/choujiang/index?home=1',
    })
  },
  opengturntable: function() {
    wx.navigateTo({
      url: '/pages/main/turntable/index?home=1',
    })
  },
  opengchallenge: function() {
    wx.navigateTo({
      url: '/pages/other/challenge/index?home=1',
    })
  },
  opengred: function() {
    wx.navigateTo({
      url: '/pages/other/redbag/index?home=1',
    })
  },
  xrlqs: function() {
    const that = this;
    that.setData({
      zanwu: true,
      measure: true,
      clicked: false,
      clim: false,
    },()=>{
      if (interstitialAd) {
        interstitialAd.show().catch((err) => {
          console.error(err)
        })
      }
    })
  },
  sfxr: util.throttle(function() {
    const that = this;
    that.setData({
      zllqcg: true,
      zlclickedcc: false,
    },()=>{
      if (interstitialAd) {
        interstitialAd.show().catch((err) => {
          console.error(err)
        })
      }
    })
    that.onShow();
  }, 1500),

  xrlq: function() {
    const that = this;
    that.setData({
      newUser: true,
      newuserclick: false,
    })
  },
  chengzhong: util.throttle(function(e) {
    const that = this;
    that.data.form_id = e.detail.formId;
    that.collectFormid();
    var object = {
      unionid: wx.getStorageSync('openid'),
    }
    var sign = app.sort_ASCII(object)
    wx.request({
      url: app.globalData.new_url + '/userinfo/Userinfo/new_welfare',
      data: {
        unionid: wx.getStorageSync('openid'),
        sign: sign
      },
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      success: function(res) {
        that.setData({
          measure: true,
          zanwu: true,
          clicked: false,
          clim: false,
        })
        if (that.data.everything.is_new) {
          that.onShow(function(res) {
            that.setData({
              zllqcg: false,
            })
          });
        } else {
          wx.navigateTo({
            url: '/pages/main/xrGoods/index',
          })
        }
      }
    })
  }, 1500),
  gotoZhuanpan: util.throttle(function() {
    var that = this;
    if (wx.getStorageSync('openid') && wx.getStorageSync('open_id') != 0) {
      wx.navigateTo({
        url: '../../main/turntable/index',
      })
    } else {
      that.authorizeNow();
    }
  }, 1500),
  zlDetail: util.throttle(function(e) {
    var form_id = e.detail.formId;
    var id = e.detail.target.dataset.id;
    wx.navigateTo({
      url: '../../main/oneDetail/index?id=' + id + '&&form_id=' + form_id + '&&parements=ss',
    })
  }, 1500),
  xrDetail: util.throttle(function(e) {
    var form_id = e.detail.formId;
    var id = e.detail.target.dataset.id;
    wx.navigateTo({
      url: '../../main/newDetail/index?id=' + id + '&&form_id=' + form_id + '&&parements=ss',
    })
  }, 1500),
  freeDetail: util.throttle(function(e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../../main/detail/index?id=' + id + '&&parements=ss',
    })
  }, 1500),
  seeMore: function() {
    wx.navigateTo({
      url: '../../main/moreGoods/index',
    })
  },
  seezlMore: function() {
    wx.navigateTo({
      url: '../../main/zlGoods/index',
    })
  },
  seexrMore: function() {
    wx.navigateTo({
      url: '../../main/xrGoods/index',
    })
  },
  gotoRule: function() {
    wx.navigateTo({
      url: '../../main/rule/index',
    })
  },

  shouquan: function() {
    const that = this;
    that.setData({
      shouquan: true,
      shouIndex: false,
    })
  },
  random: function(e) {
    const that = this;
    const plate = e.target.dataset.plate;
    that.data.seat = e.target.dataset.seat;
    that.data.plate = plate;
    var ad_id = that.data.ad_id;
    if (wx.getStorageSync('openid')) {
      var object = {
        unionid: wx.getStorageSync('openid'),
        ad_id: ad_id
      }
      var sign = app.sort_ASCII(object)
      wx.request({
        url: app.globalData.new_url + '/userinfo/Userinfo/home_lucky_ad',
        data: {
          unionid: wx.getStorageSync('openid'),
          ad_id: ad_id,
          sign: sign
        },
        method: 'POST',
        header: {
          'content-type': 'application/json'
        },
        success: function(res) {
          console.log('幸运奖励', res)
          that.data.ra = res.data.rand_rlb;
          if (res.data.ad.way == 1) {
            that.setData({
              luck: false,
              ad_data: res.data.ad
            })
          } else {
            that.setData({
              ad_data: res.data.ad,
            })
            var ad_id = res.data.ad.ad_id;
            var ra = res.data.rand_rlb
            wx.navigateTo({
              url: '../../other/erweima/index?ad_id=' + ad_id + '&&plate=' + plate + '&&rand_rlb=' + ra,
            })
          }
        }
      })
    } else {
      return
    }
  },
 
  openluck: util.throttle(function(e) {
    const that = this;
    that.data.signbiao = 1;
    videoAd.show().catch(err => {
      //失败重试
      videoAd.load().then(() => {
        videoAd.show();
      })
    })
    that.setData({
      stepdh: true,
      clickedsuc:false,
      trysuc:true,
      share:true,
    })
  }, 1500),

  opengz: function() {
    this.setData({
      gzjl: false,
    })
  },
  opengjinqun: function() {
    wx.navigateTo({
      url: '/pages/other/jinqun/index',
    })
  },
  openadd: function() {
    let that = this;
    var object = {
      unionid: wx.getStorageSync('openid'),
    }
    var sign = app.sort_ASCII(object)
    wx.request({
      url: app.globalData.new_url + '/userinfo/Userinfo/is_add',
      data: {
        unionid: wx.getStorageSync('openid'),
        sign: sign
      },
      method: 'POST',
      success: function(res) {
        that.setData({
          tjjl: false
        })
      }
    })
  },
  continueplay: function() {
    const that = this;
    if (that.data.seat == 12) {
      that.setData({
        sign: false,
        tryfail: true,
        clickedfail: false
      })
    } else if (that.data.seat == 11) {
      that.setData({
        stepdh: false,
        tryfail: true,
        clickedfail: false
      })
    } else if (that.data.seat == 13) {
      that.setData({
        luck: false,
        tryfail: true,
        clickedfail: false
      })
    }
  },
  addclick: function() {
    const that = this;
    that.setData({
      tjjl: true,
      addmy: false,
    })
  },
  gotochou: util.throttle(function() {
    var that = this;
    if (wx.getStorageSync('openid') && wx.getStorageSync('open_id') != 0) {
      wx.navigateTo({
        url: '/pages/other/choujiang/index'
      })
    } else {
      that.authorizeNow();
    }
  }, 1500),
  gotoredbag: util.throttle(function() {
    var that = this;
    if (wx.getStorageSync('openid') && wx.getStorageSync('open_id') != 0) {
      wx.navigateTo({
        url: '/pages/other/redbag/index'
      })
      that.setData({
        zllqcg: true,
        zlclickedcc: false,
      })
    } else {
      that.authorizeNow();
    }
  }, 1500),
  gzclick: function() {
    const that = this;
    that.setData({
      gzjl: true,
      contact: false,
    })
  },
  hideHandlead: function() {
    const that = this;
    that.setData({
      share: true,
    })
    if (interstitialAd) {
      interstitialAd.show().catch((err) => {
        console.error(err)
      })
    }
  },
  hideHandle: function() {
    const that = this;
    that.setData({
      sign: true,
      addmy: true,
      contact: true,
      tjjl: true,
      gzjl: true,
      luck: true,
      tryfail: true,
      clickedfail: false,
      stepdh: true,
      dhcg: false,
      signfail: true,
      perMessage: true,
    })
  },
  hidesuc: function() {
    const that = this;
    that.setData({
      trysuc: true,
      clickedsuc: false,
    })
    that.onShow();
  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function(res) {
    var openid = wx.getStorageSync('openid')
    return {
      title: '步数换好礼，我正在用步数免费领礼品，你也快来！',
      imageUrl: '../../../imgs/share.png',
      path: '/pages/tarbar/index/index?openid=' + openid + '&&jx=55'
    }
  },
})